let app = getApp();
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: Array,
      value: []
    },
    defaultConfig: {
      type: Object,
      value: {}
    }
  },
  //组件生命周期
  lifetimes: {
    ready: function () {},
    attached: function () {
      this.init()
    },
    detached: function () {}
  },
  /**
   * 组件内私有数据
   */
  data: {
    activeIdx: -1,
    flag: true
  },
  /**
   * 组件的公有方法列表
   */
  methods: {
    init() {
      console.log(this.data.list)
    },
    play() {
      let _this = this
      if (!_this.data.flag) {
        return false
      }
      _this.setData({
        flag: false
      })
      let midIdx = parseInt(this.data.list.length / 2)
      let x = -1
      let listing = [0, 1, 2, 5, 8, 7, 6, 3]
      let timer = setInterval(() => {
        x++
        if (x > listing.length - 1) {
          x = 0
        }
        _this.setData({
          activeIdx: listing[x]
        })
      }, 100)

      // 模拟 后台返回中奖
      function getLucky() {
        setTimeout(() => {
          let luckyId = _this.randomNum(0, 7) // 中奖id
          console.log('luckyId:' + luckyId)
          if (luckyId == undefined) {
            getLucky()
          } else {
            clearInterval(timer)
            let luckyIdx = _this.data.list.findIndex(item => {
              return item.id == luckyId
            })
            console.log('奖品数组序号：' + luckyIdx)
            let luckyIdx2 = listing.findIndex(item => {
              return item == luckyIdx
            })
            console.log('中奖序号：' + luckyIdx2)

            let n = 100
            let time // 最后中奖滚动间隔
            let num = 0 // 中奖最少滚次数

            function end() {
              let timer2 = setTimeout(() => {
                if (Math.abs(luckyIdx2 - x) > 4) {
                  if (n < 400) {
                    time = 300
                  } else if (n < 800) {
                    time = 500
                  } else {
                    time = 800
                  }
                } else {
                  if (n < 3000) {
                    time = 300
                  } else if (n < 20000) {
                    time = 500
                  } else {
                    time = 800
                  }
                }
                x++
                num++
                n = n * 2
                if (x > listing.length - 1) {
                  x = 0
                }
                // 结束
                if (luckyIdx2 == x && num > 4) { // 中奖最少滚次数
                  clearTimeout(timer2)
                  _this.triggerEvent('customevent', _this.data.list[luckyIdx])
                  _this.setData({
                    flag: true
                  })
                } else {
                  end()
                }
                _this.setData({
                  activeIdx: listing[x]
                })
              }, time)
            }
            end()
          }
        }, 3000)
      }
      getLucky()
    },
    randomNum(minNum, maxNum) {
      switch (arguments.length) {
        case 1:
          return parseInt(Math.random() * minNum + 1, 10);
          break;
        case 2:
          return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
          break;
        default:
          return 0;
          break;
      }
    }
  }
})